<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class CLdapUser implements IUserSourceInterface
{
    private $_initialized = false;
    
    public function  __construct()
    {
    }


    public function getUser($userInfo){
        $userName = $userInfo['userName'];
        $password = $userInfo['password'];
        $option   = Option::model()->find("option_name='ldap_host'");
        $ldapHost = $option["option_value"];
        $option   = Option::model()->find("option_name='ldap_port'");
        $ldapPort = $option["option_value"];
        $option   = Option::model()->find("option_name='ldap_base_cn'");
        $ldapBaseCn = $option["option_value"];
                $ldap_conn = ldap_connect($ldapHost, $ldapPort);
        $bind = array();
        
                ldap_set_option($ldap_conn,LDAP_OPT_PROTOCOL_VERSION,3);
        
        
        ldap_set_option($ldap_conn,LDAP_OPT_REFERRALS,0);
                $bind['result'] = @ldap_bind($ldap_conn);        if (!$bind['result'])
        {
          return false;
        }
        $result = @ldap_search($ldap_conn, $ldapBaseCn, '(&(uid='.$userName.')(userpassword='.$password.'))');
        if(!$result){
        	return false;
        }
                $info = ldap_get_entries($ldap_conn, $result);
        if ($info["count"] <= 0)
        {
            return false;
        }
        $mail = $info[0]["mail"][0];
        @ldap_close($ldap_conn);

        $retData = array();
        $retData['user_name'] = $userName;
        $retData['email']     = $mail;
        return $retData;
    }

    
    public function judgeSelf(){
        return true;
    }

}
?>